package cn.iocoder.yudao.module.polling.controller.admin.pollingplan;

import cn.iocoder.yudao.module.polling.controller.admin.pollingcategories.vo.EquVo;
import cn.iocoder.yudao.module.polling.controller.admin.pollingplan.vo.*;
import cn.iocoder.yudao.module.polling.convert.ids.OrderUtil;
import cn.iocoder.yudao.module.polling.convert.pollingplan.PollingPlanConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingplan.PollingPlanDO;
import cn.iocoder.yudao.module.polling.service.pollingplan.PollingPlanService;
import com.xkcoding.http.util.StringUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Api(tags = "管理后台 - 巡检计划")
@RestController
@RequestMapping("/abu/polling-plan")
@Validated
public class PollingPlanController {

    @Resource
    private PollingPlanService pollingPlanService;

    @PostMapping("/create")
    @ApiOperation("创建巡检计划")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:create')")
    public CommonResult<Integer> createPollingPlan(@Valid @RequestBody PollingPlanCreateReqVO createReqVO) {
        createReqVO.setPlanCode(OrderUtil.getId("XH"));
        return success(pollingPlanService.createPollingPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检计划")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:update')")
    public CommonResult<Boolean> updatePollingPlan(@Valid @RequestBody PollingPlanUpdateReqVO updateReqVO) {
        pollingPlanService.updatePollingPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:delete')")
    public CommonResult<Boolean> deletePollingPlan(@RequestParam("id") Integer id) {
        pollingPlanService.deletePollingPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:query')")
    public CommonResult<PollingPlanRespVO> getPollingPlan(@RequestParam("id") Integer id) {
        PollingPlanDO pollingPlan = pollingPlanService.getPollingPlan(id);
        return success(PollingPlanConvert.INSTANCE.convert(pollingPlan));
    }

    @GetMapping("/list")
    @ApiOperation("获得巡检计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:query')")
    public CommonResult<List<PollingPlanRespVO>> getPollingPlanList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingPlanDO> list = pollingPlanService.getPollingPlanList(ids);
        return success(PollingPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检计划分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:query')")
    public CommonResult<PageResult<PollingPlanRespVO>> getPollingPlanPage(@Valid PollingPlanPageReqVO pageVO) {
        PageResult<PollingPlanDO> pageResult = pollingPlanService.getPollingPlanPage(pageVO);
        return success(PollingPlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/myList")
    @ApiOperation("获得巡检计划")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:querymy')")
    public CommonResult<PageResult<PollingPlanInVo>> getPollingPlanPage(@Valid PollingPlanInVo pageVO) {
        if (!StringUtil.isEmpty(pageVO.getCreator())) {
            pageVO.setUserId(getLoginUserId().intValue());
        }
        List<PollingPlanInVo> list = pollingPlanService.selectPlanList(pageVO);
        PageResult<PollingPlanInVo> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }

    @GetMapping("/myListInfo")
    @ApiOperation("获得巡检计划imei")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:queryDetails')")
    public CommonResult<PageResult<PollingPlanInDao>> getPollingPlanDetails(@Valid PollingPlanInDao pageVO) {
        List<PollingPlanInDao> list = pollingPlanService.selectDetailsList(pageVO);
        PageResult<PollingPlanInDao> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }



    @GetMapping("/myUserList/{id}")
    @ApiOperation("获得巡检计划用户")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:queryuser')")
    public CommonResult<PageResult<EquVo>> getPollingPlanUserPage(@PathVariable("id") Integer id) {
        List<EquVo> list = pollingPlanService.selectUserByPlanId(id);
        PageResult<EquVo> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出巡检计划 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingPlanExcel(@Valid PollingPlanExportReqVO exportReqVO,
                                       HttpServletResponse response) throws IOException {
        List<PollingPlanDO> list = pollingPlanService.getPollingPlanList(exportReqVO);
        // 导出 Excel
        List<PollingPlanExcelVO> datas = PollingPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检计划.xls", "数据", PollingPlanExcelVO.class, datas);
    }

}
